package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zygljg;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class ZygljgForm extends BasePage implements Serializable {
    private GenericManager<Zygljg, Long> zygljgManager;
    private Zygljg zygljg = new Zygljg();
    private Long id;
    private Jbqk jbqk;
    public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}
    public void setZygljgManager(GenericManager<Zygljg, Long> zygljgManager) {
        this.zygljgManager = zygljgManager;
    }

    public Zygljg getZygljg() {
        return zygljg;
    }

    public void setZygljg(Zygljg zygljg) {
        this.zygljg = zygljg;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zygljgManager.remove(zygljg.getId());
        addMessage("zygljg.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zygljg = zygljgManager.get(id);
        } else {
            zygljg = new Zygljg();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zygljg.getId() == null || zygljg.getId() == 0);
        
        zygljg.setJbqk(jbqk);
        zygljg.setUsername(getRequest().getRemoteUser());
        zygljg.setOptdate(new Date());
        zygljg.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        zygljgManager.save(zygljg);

        String key = (isNew) ? "zygljg.added" : "zygljg.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
          	
     	   this.id=this.zygljg.getId();
              return edit();

        }
    }
} 